// 任务分组控制器
import taskGroupService from '../services/taskGroupService.ts';
import { successResponse, errorResponse } from '../utils/response.ts';
import type { FastifyRequest, FastifyReply } from 'fastify';

// 定义创建任务分组请求体类型
interface CreateTaskGroupBody {
  name: string;
}

// 定义更新任务分组请求体类型
interface UpdateTaskGroupBody {
  name: string;
}

// 定义路由参数类型
interface TaskGroupParams {
  groupId: string;
}

/**
 * 创建任务分组控制器
 * 处理创建新任务分组的请求
 * @param {FastifyRequest} request - Fastify请求对象，包含分组名称
 * @param {FastifyReply} reply - Fastify响应对象
 * @returns {Promise<FastifyReply>} 创建结果响应
 */
async function createTaskGroup(request: FastifyRequest<{ Body: CreateTaskGroupBody }>, reply: FastifyReply): Promise<FastifyReply> {
  try {
    const { name } = request.body;
    const userId = request.user?.userId; // 从认证中间件获取用户ID
    if (!userId) {
      return reply.code(401).send(errorResponse('用户未登录', 401));
    }
    const result = await taskGroupService.createTaskGroup(userId, name);
    return reply.send(successResponse(result, '创建成功'));
  } catch (err) {
    return reply.code(400).send(errorResponse((err as Error).message, 10001));
  }
}

/**
 * 获取任务分组列表控制器
 * 处理获取用户任务分组列表的请求
 * @param {FastifyRequest} request - Fastify请求对象
 * @param {FastifyReply} reply - Fastify响应对象
 * @returns {Promise<FastifyReply>} 任务分组列表响应
 */
async function getTaskGroups(request: FastifyRequest, reply: FastifyReply): Promise<FastifyReply> {
  try {
    const userId = request.user?.userId; // 从认证中间件获取用户ID
    if (!userId) {
      return reply.code(401).send(errorResponse('用户未登录', 401));
    }
    const result = await taskGroupService.getTaskGroups(userId);
    return reply.send(successResponse(result, '获取成功'));
  } catch (err) {
    return reply.code(500).send(errorResponse((err as Error).message, 10006));
  }
}

/**
 * 更新任务分组控制器
 * 处理更新任务分组信息的请求
 * @param {FastifyRequest} request - Fastify请求对象，包含分组ID和新名称
 * @param {FastifyReply} reply - Fastify响应对象
 * @returns {Promise<FastifyReply>} 更新结果响应
 */
async function updateTaskGroup(request: FastifyRequest<{ Params: TaskGroupParams; Body: UpdateTaskGroupBody }>, reply: FastifyReply): Promise<FastifyReply> {
  try {
    const { groupId } = request.params;
    const { name } = request.body;
    const userId = request.user?.userId; // 从认证中间件获取用户ID
    if (!userId) {
      return reply.code(401).send(errorResponse('用户未登录', 401));
    }
    const result = await taskGroupService.updateTaskGroup(userId, parseInt(groupId), name);
    return reply.send(successResponse(result, '更新成功'));
  } catch (err) {
    if ((err as Error).message === 'Task group not found or access denied') {
      return reply.code(404).send(errorResponse((err as Error).message, 10004));
    }
    return reply.code(400).send(errorResponse((err as Error).message, 10001));
  }
}

/**
 * 删除任务分组控制器
 * 处理删除任务分组的请求
 * @param {FastifyRequest} request - Fastify请求对象，包含分组ID参数
 * @param {FastifyReply} reply - Fastify响应对象
 * @returns {Promise<FastifyReply>} 删除结果响应
 */
async function deleteTaskGroup(request: FastifyRequest<{ Params: TaskGroupParams }>, reply: FastifyReply): Promise<FastifyReply> {
  try {
    const { groupId } = request.params;
    const userId = request.user?.userId; // 从认证中间件获取用户ID
    if (!userId) {
      return reply.code(401).send(errorResponse('用户未登录', 401));
    }
    await taskGroupService.deleteTaskGroup(userId, parseInt(groupId));
    return reply.send(successResponse({}, '删除成功'));
  } catch (err) {
    if ((err as Error).message === 'Task group not found or access denied') {
      return reply.code(404).send(errorResponse((err as Error).message, 10004));
    }
    return reply.code(400).send(errorResponse((err as Error).message, 10001));
  }
}

export default { createTaskGroup, getTaskGroups, updateTaskGroup, deleteTaskGroup };



